// 1. 导入
const fs = require('fs')
const path = require('path')
const http = require('http')

// 2. 创建web 服务器实例
const server = http.createServer()

// 3. 启动服务器
server.listen(3000, () => {
    console.log(`server is running .... at 3000`)
})

// 4. 为服务器实例绑定
server.on('request', (req, res) => {
    res.statusCode = 200
    console.log(req.url)
    let url = req.url
    if (url === '/' || url === '/index.html') {
        url = '/index.html'
        // const fullPath = path.join(__dirname, './clock/index.html')
        // const resFile = fs.readFileSync(fullPath)
        // res.end(resFile)
    }

    const fullPath = path.join(__dirname, 'www', url)
    try {
        // 报错是因为favicon.ico 找不到
        const resFile = fs.readFileSync(fullPath)
        res.end(resFile)
    } catch (err) {
        res.end('404 not found')
    }




    // else if (req.url === '/clock.css') {
    //     const fullPath = path.join(__dirname, './clock/clock.css')
    //     const resFile = fs.readFileSync(fullPath)
    //     res.end(resFile)
    // } else if (req.url === '/clock.js') {
    //     const fullPath = path.join(__dirname, './clock/clock.js')
    //     const resFile = fs.readFileSync(fullPath)
    //     res.end(resFile)
    // }
})